package com.nhh.v.service

import java.util.Date
import java.{lang ⇒ jl, math ⇒ jm, util ⇒ jul}

import scala.collection.JavaConverters._

import com.nhh.v.cons.ServerCons
import com.nhh.v.mapper.{NhhwakCoinDetailsMapper, NhhwakUsersMapper}
import com.nhh.v.utils.FormatterUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
  * Created by ywp on 2018/9/3.
  */
@Service
class CoinsService(@Autowired val nhhwakUsersMapper: NhhwakUsersMapper,
                   @Autowired val nhhwakCoinDetailsMapper: NhhwakCoinDetailsMapper
                  ) extends BaseService {

  def qrCoinsFlow(uid: String, pIndex: String = "0", pSize: String = "20"): Seq[CoinsFlow] = {
    startPage(pIndex.toInt, pSize.toInt, false)
    val coinDetails = nhhwakCoinDetailsMapper.qrCoinsFlow(uid).asScala
    coinDetails.map(cnd ⇒ {
      CoinsFlow(cnd.getId.toString, cnd.getRecvtime, cnd.getSource, FormatterUtil.format(cnd.getNhhcoins), ServerCons.sourceFm.asScala.getOrElse(cnd.getSource, cnd.getSource))
    })

  }

  def getCoinsInfo(uid: String): CoinsInfo = {

    val nhhCoins: BigDecimal = Option(nhhwakUsersMapper.selectNhhCoinsById(uid.toInt)) match {
      case None ⇒ 0
      case Some(nc) ⇒ nc
    }
    val nhhMoney = FormatterUtil.format(nhhCoins * ServerCons.nhhPrice)
    CoinsInfo(FormatterUtil.format(nhhCoins), ServerCons.nhhPrice, nhhMoney, s"总资产(¥) ≈$nhhMoney")

  }

  def qrMyCoins(uid: String, source: String = "wak"): Seq[MyCoins] = {

    val myCoins = nhhwakCoinDetailsMapper.qrMyCoins(uid, source).asScala
    myCoins.map(c ⇒ {
      MyCoins(c.getId.toString, c.getCalctime, source, FormatterUtil.format(c.getNhhcoins), c.getCreatetime)
    })

  }

  def recvNhhCoins(coinIds: String): RecvNhhCoins = {

    RecvNhhCoins("", "", "")
  }

}

case class CoinsInfo(nhhcoins: String, nhhprice: Double = ServerCons.nhhPrice, nhhmoney: String, nhhmoneyDesc: String)

case class CoinsFlow(id: String, recvtime: Date, source: String, nhhcoins: String, sourceFm: String)

case class MyCoins(id: String, calctime: Date, source: String, nhhcoins: String, createtime: Date)

case class RecvNhhCoins(retCode: String, retMsg: String, userNhhCoins: String)

